/*
* This file is a part of the open-eBackup project.
* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
* If a copy of the MPL was not distributed with this file, You can obtain one at
* http://mozilla.org/MPL/2.0/.
*
* Copyright (c) [2024] Huawei Technologies Co.,Ltd.
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*/
import { CommonModule } from '@angular/common';
import { Component, Input, OnInit } from '@angular/core';
import { AbstractControl, FormControl, FormGroup } from '@angular/forms';
import { BaseUtilService, DataMapService, I18NService } from 'app/shared';
import { BaseModule } from 'app/shared/base.module';
import { has } from 'lodash';

@Component({
  selector: 'aui-control-file',
  standalone: true,
  imports: [BaseModule, CommonModule],
  templateUrl: './control-file.component.html',
  styleUrls: ['./control-file.component.less']
})
export class ControlFileComponent implements OnInit {
  @Input() formGroup: FormGroup;
  @Input() resourceData;
  @Input() isDetail = false;
  @Input() protectData;
  countErrorTip = {
    ...this.baseUtilService.requiredErrorTip,
    invalidInteger: this.i18n.get('common_valid_positive_integer_label'),
    invalidRang: this.i18n.get('common_valid_rang_label', [50000, 10000000])
  };
  sizeErrorTip = {
    ...this.baseUtilService.requiredErrorTip,
    invalidInteger: this.i18n.get('common_valid_positive_integer_label'),
    invalidRang: this.i18n.get('common_valid_rang_label', [1, 1000])
  };

  constructor(
    private i18n: I18NService,
    private dataMapService: DataMapService,
    public baseUtilService: BaseUtilService
  ) {}

  ngOnInit() {
    this.initForm();
    this.updateData();
  }

  initForm() {
    this.formGroup.addControl(
      'fileCountThreshold',
      new FormControl(100000, {
        validators: [
          this.baseUtilService.VALID.required(),
          this.integer(),
          this.baseUtilService.VALID.rangeValue(50000, 10000000)
        ]
      })
    );
    this.formGroup.addControl(
      'fileSizeThreshold',
      new FormControl(10, {
        validators: [
          this.baseUtilService.VALID.required(),
          this.integer(),
          this.baseUtilService.VALID.rangeValue(1, 1000)
        ]
      })
    );
  }

  integer() {
    return (control: AbstractControl): { [key: string]: any } | null => {
      if (!control.value) {
        return null;
      }

      return !/^[1-9]\d*$/.test(control.value)
        ? { invalidInteger: { value: control.value } }
        : null;
    };
  }

  updateData() {
    if (has(this.resourceData, 'protectedObject.extParameters')) {
      const extParameters: any =
        this.resourceData.protectedObject.extParameters || {};
      this.formGroup
        .get('fileSizeThreshold')
        .setValue(extParameters?.fileSizeThreshold || 10);
      this.formGroup
        .get('fileCountThreshold')
        .setValue(extParameters?.fileCountThreshold || 100000);
    }
  }
}
